package com.bfs.anasit.jsfbean;

import com.bfs.anasit.model.Patient;
import com.bfs.anasit.model.Record;
import com.bfs.anasit.service.PatientService;
import com.bfs.anasit.service.RecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.inject.Model;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: lukman
 * Date: 8/5/11
 * Time: 3:42 AM
 * To change this template use File | Settings | File Templates.
 */
@Model
public class RecordBean implements Serializable {
    @Inject
    private static Logger log;
    @Inject
    private Record record;

    private Patient selectedPatient;
    private List<Patient> patientList;

    private List<Record> recordList;
    @Inject
    private RecordService recordService;
    @Inject
    private PatientService patientService;

    public Patient getSelectedPatient() {
        return selectedPatient;
    }

    public RecordService getRecordService() {
        return recordService;
    }

    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }

    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }

    public void setSelectedPatient(Patient selectedPatient) {
        this.selectedPatient = selectedPatient;
    }

    public Record getRecord() {
        return record;
    }

    public void setRecord(Record record) {
        this.record = record;
    }

    public List<Record> getRecordList() {
        return recordList;
    }

    public void setRecordList(List<Record> recordList) {
        this.recordList = recordList;
    }

    public List<Patient> completePatient(String query) {
        List<Patient> patients = patientList;
        List<Patient> suggestions = new ArrayList<Patient>();
        for (Patient p : patients) {
            if (p.getName().getFirstName().startsWith(query) || p.getName().getFirstName().startsWith(query) || p.getName().getLastName().startsWith(query))
                suggestions.add(p);
        }

        return suggestions;
    }

    public List<Patient> getPatientList() {
        patientList = patientService.findAllPatients();
        return patientList;
    }


    public List<SelectItem> getAlteredPatientList() {
        List<SelectItem> alteredPatientList = new ArrayList<SelectItem>();
        alteredPatientList.add(new SelectItem(null, "Select Patient", "Description", false, false, true));
        for (Patient p : getPatientList()) {
            alteredPatientList.add(new SelectItem(p.getPatientId(), p.getName().getFirstName() + " " + p.getName().getLastName()));
        }
        return alteredPatientList;
    }


    public void setPatientList(List<Patient> patientList) {
        this.patientList = patientList;
    }

    public void createRecord() {

    }
}
